﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace FWMS
{
    public partial class Form_churukutaizhang : Form
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
        [DllImport("kernel32.dll")]
        public static extern void fclose(IntPtr hObject);
        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);
        public const int OF_READWRITE = 2;
        public const int OF_SHARE_DENY_NONE = 0x40;
        public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
        public Form_churukutaizhang()
        {
            InitializeComponent();
        }
        //类型选择
        private void Form_churukutaizhang_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            this.comboBox1.Items.Add("原料");
            this.comboBox1.Items.Add("调味料");
            this.comboBox1.Items.Add("辅料");
            this.comboBox1.Items.Add("成品");
            this.comboBox1.SelectedItem = "原料";
            string nowdate = DateTime.Now.ToString("yyyyMMdd");
            string date = nowdate.ToString().Substring(0, 4) + "年" +
                    nowdate.ToString().Substring(4, 2) + "月" +
                    nowdate.ToString().Substring(6, 2) + "日";
            this.label4.Text = date;
            this.comboBox1.Focus();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            import_excel();
        }
        private void import_excel()
        {
            Service.Taizhangguanli taizhangservice = new FWMS.Service.Taizhangguanli();


            string type = (string)this.comboBox1.SelectedItem;
            switch (type.Trim())
            {
                case "原料":
                    if (taizhangservice.taizhangguanli_zhuliucheng("0"))
                    {
                        yuanliaoexportexcel(taizhangservice.taizhangguanli_taizhanggetneeddata("0"));
                    }
                    else
                    {
                        MessageBox.Show("台帐运行出错！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                case "调味料":
                    if (taizhangservice.taizhangguanli_zhuliucheng("1"))
                    {
                        yuanliaoexportexcel(taizhangservice.taizhangguanli_taizhanggetneeddata("1"));
                    }
                    else
                    {
                        MessageBox.Show("台帐运行出错！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                case "辅料":
                    if (taizhangservice.taizhangguanli_zhuliucheng("2"))
                    {
                        yuanliaoexportexcel(taizhangservice.taizhangguanli_taizhanggetneeddata("2"));
                    }
                    else
                    {
                        MessageBox.Show("台帐运行出错！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                case "成品":
                    if (taizhangservice.taizhangguanli_getchanpinzhuliucheng("3"))
                    {
                        yuanliaoexportexcel(taizhangservice.taizhangguanli_taizhanggetchanpindata());
                    }
                    else
                    {
                        MessageBox.Show("台帐运行出错！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                default: break;

            }

        }
        private void yuanliaoexportexcel(Bean.KucunguanliBean bean)
        {
            if (bean.Cailiaoresult == null)
            {
                MessageBox.Show("目前无台帐！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string taizhangname = "";
            string taizhangsheetname = "";
            switch ((string)this.comboBox1.SelectedItem)
            {
                case "原料": taizhangname = "原料台帐MODEL.xls"; taizhangsheetname = "原料台帐"; break;
                case "调味料": taizhangname = "调味料台帐MODEL.xls"; taizhangsheetname = "调味料台帐"; break;
                case "辅料": taizhangname = "辅料台帐MODEL.xls"; taizhangsheetname = "辅料台帐"; break;
                case "成品": taizhangname = "成品台帐MODEL.xls"; taizhangsheetname = "成品台帐"; break;
                default: break;
            }
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string path2 = "";
            saveFileDialog1.Filter = "EXCEL files(*.xls)|*.xls";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            //保存EXCEL
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path2 = saveFileDialog1.FileName.ToString();
            }
            else
            {
                return;
            }
            saveFileDialog1.Dispose();

            string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            if (File.Exists(path2))
            {
                IntPtr vHandle = _lopen(path2, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vHandle == HFILE_ERROR)
                {
                    MessageBox.Show("该文件已经打开，请关闭后重新导出！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    CloseHandle(vHandle);
                    File.Delete(path2);
                }
            }
            string pinming = "";
            string guige = "";
            string minxienddate = "";
            string minxibeizhu = "";
            //importexcel.copyRows(importexcel.GetSheet(taizhangsheetname),"A1","L5","A8","L12");
            COMMAND.CommandMicrosoftExcel importexcel = new FWMS.COMMAND.CommandMicrosoftExcel();
            importexcel.Open(path + taizhangname);
            int i = 0;
            int excelline = 6;
            int linespace = 0;
            linespace = excelline;
            int monthsumjianshu = 0;
            float monthsumdanzhong = 0;
            float monthsumzhongliang = 0;
            float chukusumzhongliang = 0;
            int sumjianshu = 0;
            float sumdanzhong = 0;
            float sumzhongliang = 0;
            float yongsumzhongliangjiji = 0;
            float neisumzhongliangjiji = 0;
            float hetongsumzhongliangjiji = 0;
            float monthsumzhongliangyong = 0;
            float monthsumzhongliangnei = 0;
            float monthsumhetong = 0;
            float cha = 0;
            string[,] excelkucunzhongliang = new string[12, 2];
            string[,] hetonghaozhongliang = new string[20, 2];
            for (int init = 0; init < 12; init++)
            {
                excelkucunzhongliang[init, 0] = null;
                excelkucunzhongliang[init, 1] = null;
            }
            for (int init1 = 0; init1 < 20; init1++)
            {
                hetonghaozhongliang[init1, 0] = null;
                hetonghaozhongliang[init1, 1] = null;
            }
            //向EXCEL中添加数据
            int k = 1;
            int p = 0;
            int jindu = 0;
            this.jindutiao.Visible = true;
            lock (this)
            {
                if ("成品".Equals((string)this.comboBox1.SelectedItem))
                {
                    for (; i < bean.Cailiaoresult.Length / 15; i++)
                    {
                        jindu = (i * 100) / (bean.Cailiaoresult.Length / 15);
                        this.label5.Text = jindu.ToString() + "%";
                        this.jindutiao.Value = jindu;
                        this.jindutiao.Show();
                        Application.DoEvents();
                        if ((pinming + guige).Equals(COMMAND.Command.toString(bean.Cailiaoresult[i, 0]).Trim() + COMMAND.Command.toString(bean.Cailiaoresult[i, 1]).Trim()))//品名和规格相等时重量加
                        {
                            if (!minxienddate.Equals(COMMAND.Command.toString(bean.Cailiaoresult[i, 2]).Trim()))//另一月份
                            {
                                if (monthsumzhongliang != 0)
                                {
                                    //入库向EXCEL中写入
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 2]));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 3, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 3]));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "本月生产");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, Math.Round(monthsumdanzhong, 3));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, monthsumjianshu);
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, Math.Round(monthsumzhongliang, 3));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, "");
                                }

                                if (monthsumzhongliangyong != 0)
                                {
                                    //出库本月生产用

                                    excelline++;
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "本月生产用");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(monthsumzhongliangyong, 3));
                                }
                                if (monthsumzhongliangnei != 0)
                                {
                                    //出库内销用
                                    excelline++;
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "内销用");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(monthsumzhongliangnei, 3));
                                }
                                if(p!=0)
                                {
                                    for (int init2 = 19; init2 >= 0; init2--)
                                    {
                                        if (hetonghaozhongliang[init2, 0] == null || hetonghaozhongliang[init2, 1] == null)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            excelline++;
                                            string date;
                                            Service.Taizhangguanli service = new FWMS.Service.Taizhangguanli();
                                            date = service.taizhangguanli_getchukudata(pinming, guige, hetonghaozhongliang[init2, 0].ToString().Trim(), COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, hetonghaozhongliang[init2, 0].ToString().Trim());
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, hetonghaozhongliang[init2, 1].ToString().Trim());
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(date));
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(date));
                                            
                                        }
                                    }
                                    for (int init1 = 0; init1 < 20; init1++)
                                    {
                                        hetonghaozhongliang[init1, 0] = null;
                                        hetonghaozhongliang[init1, 1] = null;
                                    }
                                    p = 0;
                                }

                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "月计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 5]));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 6]));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 7]));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(monthsumhetong + monthsumzhongliangnei + monthsumzhongliangyong, 3));
                                excelline++;
                                hetongsumzhongliangjiji = (float)(Math.Round(hetongsumzhongliangjiji, 3) + Math.Round(monthsumhetong, 3));//出库生产合用总累计
                                yongsumzhongliangjiji = (float)(Math.Round(yongsumzhongliangjiji, 3) + Math.Round(monthsumzhongliangyong, 3));//出库生产用总累计
                                neisumzhongliangjiji = (float)(Math.Round(neisumzhongliangjiji, 3) + Math.Round(monthsumzhongliangnei, 3));//出库内销用总累计
                                minxienddate = COMMAND.Command.toString(bean.Cailiaoresult[i, 2]).Trim();
                                sumdanzhong = (float)(Math.Round(sumdanzhong, 3) + Math.Round(monthsumdanzhong, 3));
                                sumjianshu = sumjianshu + monthsumjianshu;
                                sumzhongliang = (float)(Math.Round(sumzhongliang, 3) + Math.Round(monthsumzhongliang, 3));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "累计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, Math.Round(sumdanzhong, 3));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, sumjianshu);
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, Math.Round(sumzhongliang, 3));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round((float)Math.Round(hetongsumzhongliangjiji, 3) + (float)Math.Round(yongsumzhongliangjiji, 3) + (float)Math.Round(neisumzhongliangjiji, 3), 3));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 11, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 10]).Trim());
                                //库存集计
                                excelkucunzhongliang[k, 0] = excelline.ToString();
                                excelkucunzhongliang[k, 1] = Math.Round(sumzhongliang - yongsumzhongliangjiji - neisumzhongliangjiji - hetongsumzhongliangjiji, 3).ToString().Trim();
                                k++;
                                monthsumhetong = 0;
                                monthsumdanzhong = 0;
                                monthsumjianshu = 0;
                                monthsumzhongliang = 0;
                                monthsumzhongliangyong = 0;
                                monthsumzhongliangnei = 0;
                                monthsumhetong = 0;
                                excelline++;
                            }

                            monthsumdanzhong = (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 5])), 3); //月集计加单重
                            monthsumjianshu = int.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 6]));
                            monthsumzhongliang = (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 7])), 3);
                            if ("本月生产用".Equals(COMMAND.Command.getLFCorYONG(COMMAND.Command.toString(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 13])))))
                            {
                                monthsumzhongliangyong = (float)Math.Round(monthsumzhongliangyong, 3) + (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 8])), 3);
                            }
                            else
                            {
                                if ("内销用".Equals(COMMAND.Command.getLFCorYONG(COMMAND.Command.toString(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 13])))))
                                {
                                    monthsumzhongliangnei = (float)Math.Round(monthsumzhongliangnei, 3) + (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 8])), 3);
                                }
                                else
                                {
                                    hetonghaozhongliang[p, 0] = COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 13]);
                                    hetonghaozhongliang[p, 1] = Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 8])), 3).ToString().Trim();
                                    monthsumhetong = (float)Math.Round(monthsumhetong, 3) + (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 8])), 3);
                                    p++;
                                }
                            }

                            if (i == (bean.Cailiaoresult.Length / 15 - 1))
                            {
                                if (monthsumzhongliang != 0)
                                {
                                    //入库
                                    excelline++;
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 2]));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 3, COMMAND.Command.toString(bean.Cailiaoresult[i, 3]));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "本月生产");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, Math.Round(monthsumdanzhong, 3));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, monthsumjianshu);
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, Math.Round(monthsumzhongliang, 3));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, "");
                                }
                                if (monthsumzhongliangyong != 0)
                                {
                                    //出库本月生产用
                                    excelline++;
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "本月生产用");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(monthsumzhongliangyong, 3));
                                }

                                if (monthsumzhongliangnei != 0)
                                {
                                    //出库内销用
                                    excelline++;
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "内销用");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(monthsumzhongliangnei, 3));
                                }
                                if (p != 0)
                                {
                                    for (int init2 = 19; init2 >= 0; init2--)
                                    {
                                        if (hetonghaozhongliang[init2, 0] == null || hetonghaozhongliang[init2, 1] == null)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            excelline++;
                                            string date;
                                            Service.Taizhangguanli service = new FWMS.Service.Taizhangguanli();
                                            date = service.taizhangguanli_getchukudata(pinming, guige, hetonghaozhongliang[init2, 0].ToString().Trim(), COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i, 14])));
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, hetonghaozhongliang[init2, 0].ToString().Trim());
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, hetonghaozhongliang[init2, 1].ToString().Trim());
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(date));
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(date));
                                        }
                                    }
                                    for (int init1 = 0; init1 < 20; init1++)
                                    {
                                        hetonghaozhongliang[init1, 0] = null;
                                        hetonghaozhongliang[init1, 1] = null;
                                    }
                                    p = 0;
                                }
                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "月计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, Math.Round(monthsumdanzhong, 3).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, monthsumjianshu.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, Math.Round(monthsumzhongliang, 3).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 8])), 3).ToString().Trim());

                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "累计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 11, COMMAND.Command.toString(bean.Cailiaoresult[i, 10]));

                                hetongsumzhongliangjiji = (float)Math.Round(hetongsumzhongliangjiji, 3) + (float)Math.Round(monthsumhetong, 3);//出库合同总累计
                                yongsumzhongliangjiji = (float)Math.Round(yongsumzhongliangjiji, 3) + (float)Math.Round(monthsumzhongliangyong, 3);//出库生产用总累计
                                neisumzhongliangjiji = (float)Math.Round(neisumzhongliangjiji, 3) + (float)Math.Round(monthsumzhongliangnei, 3);//出库内销用总累计
                                sumjianshu = sumjianshu + monthsumjianshu;
                                sumdanzhong = (float)Math.Round(sumdanzhong + monthsumdanzhong, 3);
                                sumzhongliang = (float)Math.Round(sumzhongliang + monthsumzhongliang, 3);

                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, Math.Round(float.Parse(COMMAND.Command.nulltozero(sumdanzhong)), 3).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, COMMAND.Command.nulltozero(sumjianshu));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, Math.Round(float.Parse(COMMAND.Command.nulltozero(sumzhongliang)), 3).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(float.Parse(COMMAND.Command.nulltozero(hetongsumzhongliangjiji +yongsumzhongliangjiji + neisumzhongliangjiji)), 3).ToString().Trim());
                                //库存集计
                                cha = (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 10])) - sumzhongliang + hetongsumzhongliangjiji + yongsumzhongliangjiji + neisumzhongliangjiji, 3);
                                for (int init2 = 11; init2 >= 0; init2--)
                                {
                                    if (excelkucunzhongliang[init2, 0] == null || excelkucunzhongliang[init2, 1] == null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), int.Parse(excelkucunzhongliang[init2, 0]), 11, Math.Round((float.Parse(excelkucunzhongliang[init2, 1]) + cha), 3).ToString());
                                    }
                                }
                                for (int init = 0; init < 12; init++)
                                {
                                    excelkucunzhongliang[init, 0] = null;
                                    excelkucunzhongliang[init, 1] = null;
                                }
                                k = 1;
                                monthsumjianshu = 0;
                                monthsumdanzhong = 0;
                                monthsumzhongliang = 0;
                                sumjianshu = 0;
                                sumdanzhong = 0;
                                sumzhongliang = 0;
                                importexcel.setCellRange(importexcel.GetSheet(taizhangsheetname), linespace, 1, excelline, 12);
                            }
                        }
                        else
                        {
                            if (i != 0)
                            {
                                if (monthsumzhongliang != 0)
                                {
                                    //入库
                                    excelline++;
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 2]));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 3, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 3]));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "本月生产");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, Math.Round(monthsumdanzhong, 3));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, monthsumjianshu);
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, Math.Round(monthsumzhongliang, 3));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, "");
                                }
                                if (monthsumzhongliangyong != 0)
                                {
                                    //出库本月生产用

                                    excelline++;
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "本月生产用");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(monthsumzhongliangyong, 3));
                                }

                                if (monthsumzhongliangnei != 0)
                                {
                                    //出库内销用
                                    excelline++;
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "内销用");
                                    importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(monthsumzhongliangnei, 3));
                                }
                                if (p != 0)
                                {
                                    for (int init2 = 19; init2 >= 0; init2--)
                                    {
                                        if (hetonghaozhongliang[init2, 0] == null || hetonghaozhongliang[init2, 1] == null)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            excelline++;
                                            string date;
                                            Service.Taizhangguanli service = new FWMS.Service.Taizhangguanli();
                                            date = service.taizhangguanli_getchukudata(pinming, guige, hetonghaozhongliang[init2, 0].ToString().Trim(), COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 14])));
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, hetonghaozhongliang[init2, 0].ToString().Trim());
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, hetonghaozhongliang[init2, 1].ToString().Trim());
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(date));
                                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(date));
                                        }
                                    }
                                    for (int init1 = 0; init1 < 20; init1++)
                                    {
                                        hetonghaozhongliang[init1, 0] = null;
                                        hetonghaozhongliang[init1, 1] = null;
                                    }
                                    p = 0;
                                }
                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "月计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, Math.Round(monthsumdanzhong, 3).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, monthsumjianshu.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, Math.Round(monthsumzhongliang, 3).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(monthsumhetong + monthsumzhongliangyong + monthsumzhongliangnei, 3).ToString().Trim());
                                excelline++;
                                hetongsumzhongliangjiji = (float)Math.Round(hetongsumzhongliangjiji, 3) + (float)Math.Round(monthsumhetong, 3);//出库生产用总累计
                                yongsumzhongliangjiji = (float)Math.Round(yongsumzhongliangjiji, 3) + (float)Math.Round(monthsumzhongliangyong, 3);//出库生产用总累计
                                neisumzhongliangjiji = (float)Math.Round(neisumzhongliangjiji, 3) + (float)Math.Round(monthsumzhongliangnei, 3);//出库内销用总累计
                                minxienddate = COMMAND.Command.toString(bean.Cailiaoresult[i, 2]).Trim();
                                sumdanzhong = (float)Math.Round(sumdanzhong, 3) + (float)Math.Round(monthsumdanzhong, 3);
                                sumjianshu = sumjianshu + monthsumjianshu;
                                sumzhongliang = (float)Math.Round(sumzhongliang, 3) + (float)Math.Round(monthsumzhongliang, 3);

                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "累计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 11, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 10]).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, Math.Round(sumdanzhong, 3).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, sumjianshu);
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, Math.Round(sumzhongliang, 3).ToString().Trim());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, Math.Round(hetongsumzhongliangjiji + yongsumzhongliangjiji + neisumzhongliangjiji, 3).ToString().Trim());
                                //库存集计
                                cha = (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 10])), 3) - (float)Math.Round(sumzhongliang, 3) + (float)Math.Round(yongsumzhongliangjiji, 3) + (float)Math.Round(neisumzhongliangjiji, 3) + (float)Math.Round(hetongsumzhongliangjiji, 3);
                                for (int init2 = 11; init2 >= 0; init2--)
                                {
                                    if (excelkucunzhongliang[init2, 0] == null || excelkucunzhongliang[init2, 1] == null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), int.Parse(excelkucunzhongliang[init2, 0]), 11, Math.Round((float.Parse(excelkucunzhongliang[init2, 1]) + cha), 3).ToString());
                                    }
                                }
                                for (int init = 0; init < 12; init++)
                                {
                                    excelkucunzhongliang[init, 0] = null;
                                    excelkucunzhongliang[init, 1] = null;
                                }
                                k = 1;
                                sumjianshu = 0;
                                sumdanzhong = 0;
                                sumzhongliang = 0;
                                monthsumjianshu = 0;
                                monthsumdanzhong = 0;
                                monthsumzhongliang = 0;
                                monthsumzhongliangyong = 0;
                                monthsumzhongliangnei = 0;
                                monthsumhetong = 0;
                                hetongsumzhongliangjiji = 0;
                                yongsumzhongliangjiji = 0;
                                neisumzhongliangjiji = 0;
                                importexcel.setCellRange(importexcel.GetSheet(taizhangsheetname), linespace, 1, excelline, 12);
                                excelline = excelline + 2;
                                importexcel.copyRows(importexcel.GetSheet(taizhangsheetname), "A1", "L5", "A" + (excelline).ToString().Trim(), "L" + (5 + excelline).ToString().Trim());//copy rows
                                excelline = excelline + 5;
                                importexcel.setRowsHeight(importexcel.GetSheet(taizhangsheetname), excelline - 5);//设置行高
                                linespace = excelline;
                            }
                            pinming = COMMAND.Command.toString(bean.Cailiaoresult[i, 0]).Trim();
                            guige = COMMAND.Command.toString(bean.Cailiaoresult[i, 1]).Trim();
                            minxienddate = COMMAND.Command.toString(bean.Cailiaoresult[i, 2]).Trim();
                            minxibeizhu = COMMAND.Command.getLFCorYONG(COMMAND.Command.toString(bean.Cailiaoresult[i, 13]).Trim());
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline - 4, 2, pinming);
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline - 4, 5, guige);
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline - 2, 1, COMMAND.Command.getyear(System.DateTime.Now.ToString("yyyyMMdd")) + "年");
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "上年转接");
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 11, "0");
                            excelkucunzhongliang[0, 0] = excelline.ToString();
                            excelkucunzhongliang[0, 1] = "0";
                            excelline++;
                            i--;
                        }
                    }
                }
                else
                {
                    for (; i < bean.Cailiaoresult.Length / 15; i++)
                    {
                        jindu = (i * 100) / (bean.Cailiaoresult.Length / 15);
                        this.label5.Text = jindu.ToString() + "%";
                        this.jindutiao.Value = jindu;
                        this.jindutiao.Show();
                        Application.DoEvents();
                        if ((pinming + guige).Equals(COMMAND.Command.toString(bean.Cailiaoresult[i, 0]).Trim() + COMMAND.Command.toString(bean.Cailiaoresult[i, 1]).Trim()))
                        {
                            if (!minxienddate.Equals(COMMAND.Command.toString(bean.Cailiaoresult[i, 14]).Trim()))
                            {

                                minxienddate = COMMAND.Command.toString(bean.Cailiaoresult[i, 14]).Trim();
                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "月计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, monthsumdanzhong.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, monthsumjianshu.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, monthsumzhongliang.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 8]));
                                chukusumzhongliang = (float)Math.Round(chukusumzhongliang + float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 8])), 3);
                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "累计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 11, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 10]).Trim());
                                sumjianshu = sumjianshu + monthsumjianshu;
                                sumdanzhong = (float)Math.Round(sumdanzhong + monthsumdanzhong, 3);
                                sumzhongliang = (float)Math.Round(sumzhongliang + monthsumzhongliang, 3);
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, COMMAND.Command.nulltozero(sumdanzhong));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, COMMAND.Command.nulltozero(sumjianshu));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, COMMAND.Command.nulltozero(sumzhongliang));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, COMMAND.Command.nulltozero(chukusumzhongliang));
                                //库存集计
                                excelkucunzhongliang[k, 0] = excelline.ToString();
                                excelkucunzhongliang[k, 1] = (sumzhongliang - chukusumzhongliang).ToString();
                                k++;
                                monthsumjianshu = 0;
                                monthsumdanzhong = 0;
                                monthsumzhongliang = 0;
                                excelline++;
                            }
                            //入库
                            if ((!"0".Equals(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 3]))) || (!"0".Equals(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 4]))) || (!"0".Equals(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 5]))))
                            {
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 1, COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i, 2])));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i, 2])));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 3, COMMAND.Command.toString(bean.Cailiaoresult[i, 3]));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, COMMAND.Command.toString(bean.Cailiaoresult[i, 4]));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 5]));
                                monthsumdanzhong = (float)Math.Round(monthsumdanzhong + float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 5])), 3); //月集计加单重
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 6]));
                                monthsumjianshu = monthsumjianshu + int.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 6]));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 7]));
                                monthsumzhongliang = (float)Math.Round(monthsumzhongliang + float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 7])), 3);
                            }
                            if (!"0".Equals(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 8])))
                            {
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, "");
                                //出库
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline + 1, 1, COMMAND.Command.getmonth(COMMAND.Command.toString(bean.Cailiaoresult[i, 14])));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline + 1, 2, COMMAND.Command.getday(COMMAND.Command.toString(bean.Cailiaoresult[i, 14])));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline + 1, 4, COMMAND.Command.toString(bean.Cailiaoresult[i, 13]));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline + 1, 9, COMMAND.Command.toString(bean.Cailiaoresult[i, 8]));
                                excelline++;
                            }


                            if (i == (bean.Cailiaoresult.Length / 15 - 1))
                            {
                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "月计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, monthsumdanzhong.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, monthsumjianshu.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, monthsumzhongliang.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 8]));
                                chukusumzhongliang = (float)Math.Round(chukusumzhongliang + float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 8])), 3);
                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "累计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 11, COMMAND.Command.toString(bean.Cailiaoresult[i, 10]));
                                sumjianshu = sumjianshu + monthsumjianshu;
                                sumdanzhong = (float)Math.Round(sumdanzhong + monthsumdanzhong, 3);
                                sumzhongliang = (float)Math.Round(sumzhongliang + monthsumzhongliang, 3);
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, COMMAND.Command.nulltozero(sumdanzhong));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, COMMAND.Command.nulltozero(sumjianshu));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, COMMAND.Command.nulltozero(sumzhongliang));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, COMMAND.Command.nulltozero(chukusumzhongliang));
                                //库存集计
                                excelkucunzhongliang[k, 0] = excelline.ToString();
                                excelkucunzhongliang[k, 1] = (sumzhongliang - chukusumzhongliang).ToString();

                                cha = (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i, 10])) - sumzhongliang + chukusumzhongliang, 3);
                                for (int init2 = 11; init2 >= 0; init2--)
                                {
                                    if (excelkucunzhongliang[init2, 0] == null || excelkucunzhongliang[init2, 1] == null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), int.Parse(excelkucunzhongliang[init2, 0]), 11, Math.Round((float.Parse(excelkucunzhongliang[init2, 1]) + cha), 3).ToString());
                                    }
                                }
                                for (int init = 0; init < 12; init++)
                                {
                                    excelkucunzhongliang[init, 0] = null;
                                    excelkucunzhongliang[init, 1] = null;
                                }
                                k = 1;
                                monthsumjianshu = 0;
                                monthsumdanzhong = 0;
                                monthsumzhongliang = 0;
                                sumjianshu = 0;
                                sumdanzhong = 0;
                                sumzhongliang = 0;
                                importexcel.setCellRange(importexcel.GetSheet(taizhangsheetname), linespace, 1, excelline, 12);
                            }
                        }
                        else
                        {
                            if (i != 0)
                            {
                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "月计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, monthsumdanzhong.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, monthsumjianshu.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, monthsumzhongliang.ToString());
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 8]));
                                chukusumzhongliang = (float)Math.Round(chukusumzhongliang + float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 8])), 3);
                                excelline++;
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "累计");
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 11, COMMAND.Command.toString(bean.Cailiaoresult[i - 1, 10]).Trim());
                                sumjianshu = sumjianshu + monthsumjianshu;
                                sumdanzhong = (float)Math.Round(sumdanzhong + monthsumdanzhong, 3);
                                sumzhongliang = (float)Math.Round(sumzhongliang + monthsumzhongliang, 3);
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 5, COMMAND.Command.nulltozero(sumdanzhong));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 6, COMMAND.Command.nulltozero(sumjianshu));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 7, COMMAND.Command.nulltozero(sumzhongliang));
                                importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 9, COMMAND.Command.nulltozero(chukusumzhongliang));
                                //库存集计
                                cha = (float)Math.Round(float.Parse(COMMAND.Command.nulltozero(bean.Cailiaoresult[i - 1, 10])) - sumzhongliang + chukusumzhongliang, 3);
                                for (int init2 = 11; init2 >= 0; init2--)
                                {
                                    if (excelkucunzhongliang[init2, 0] == null || excelkucunzhongliang[init2, 1] == null)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), int.Parse(excelkucunzhongliang[init2, 0]), 11, Math.Round((float.Parse(excelkucunzhongliang[init2, 1]) + cha), 3).ToString());
                                    }
                                }
                                for (int init = 0; init < 12; init++)
                                {
                                    excelkucunzhongliang[init, 0] = null;
                                    excelkucunzhongliang[init, 1] = null;
                                }
                                k = 1;
                                chukusumzhongliang = 0;
                                sumjianshu = 0;
                                sumdanzhong = 0;
                                sumzhongliang = 0;
                                monthsumjianshu = 0;
                                monthsumdanzhong = 0;
                                monthsumzhongliang = 0;
                                importexcel.setCellRange(importexcel.GetSheet(taizhangsheetname), linespace, 1, excelline, 12);
                                excelline = excelline + 2;
                                importexcel.copyRows(importexcel.GetSheet(taizhangsheetname), "A1", "L5", "A" + (excelline).ToString().Trim(), "L" + (5 + excelline).ToString().Trim());//copy rows
                                excelline = excelline + 5;
                                importexcel.setRowsHeight(importexcel.GetSheet(taizhangsheetname), excelline - 5);//设置行高
                                linespace = excelline;
                            }

                            pinming = COMMAND.Command.toString(bean.Cailiaoresult[i, 0]).Trim();
                            guige = COMMAND.Command.toString(bean.Cailiaoresult[i, 1]).Trim();
                            minxienddate = COMMAND.Command.toString(bean.Cailiaoresult[i, 14]).Trim();
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline - 4, 2, pinming);
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline - 4, 5, guige);
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline - 2, 1, COMMAND.Command.getyear(System.DateTime.Now.ToString("yyyyMMdd")) + "年");
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 4, "上年转接");
                            importexcel.SetCellValue(importexcel.GetSheet(taizhangsheetname), excelline, 11, "0");
                            excelkucunzhongliang[0, 0] = excelline.ToString();
                            excelkucunzhongliang[0, 1] = "0";
                            excelline++;
                            i--;
                        }
                    }
                }


            }
            jindu = (i * 100) / (bean.Cailiaoresult.Length / 15);
            this.jindutiao.Visible = false;
            this.label5.Visible = false;
            importexcel.SaveAs(path2);
            importexcel.Close();
            MessageBox.Show("导出EXCEL成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


    }
}
